<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>分段专题图</title>

    <link href="http://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="http://cdn.bootcss.com/leaflet/1.0.3/leaflet.css">
    <script type="text/javascript" src="http://cdn.bootcss.com/leaflet/1.0.3/leaflet.js"></script>
    <script type="text/javascript" src="http://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
    <script type="text/javascript" src="../../dist/iclient9-leaflet.min.js"></script>
    <style type="text/css">
        .legendItemHeader,
        .legendItemValue {
            width: 120px;
            height: 18px;
            font-size: 14px;
        }
    </style>
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
<div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
<script type="text/javascript">
    var map, themeLayer, infoView,
        baseUrl = "http://117.122.248.69:8090/iserver/services/map-jingjin/rest/maps/京津地区地图",
        dataUrl = "http://117.122.248.69:8090/iserver/services/data-jingjin/rest/data";

    init();
    function init() {
        // 检测是否支持 Canvas
        if (!document.createElement('canvas').getContext) {
            alert("您的浏览器不支持 Canvas，请升级！");
            return;
        }

        map = L.map("map", {
            crs: L.CRS.EPSG4326,
            center: [40, 117],
            maxZoom: 18,
            zoom: 7
        });
        L.supermap.tiledMapLayer(baseUrl).addTo(map);
        initThemeLayer();
    }
    function initThemeLayer() {
        themeLayer = L.supermap.rangeThemeLayer("ThemeLayer", {
            // 开启 hover 高亮效果
            isHoverAble: true,
            opacity: 0.8
        }).addTo(map);

        themeLayer.style = new SuperMap.ThemeStyle({
            shadowBlur: 16,
            shadowColor: "#000000",
            fillColor: "#FFFFFF"
        });

        // hover 高亮样式
        themeLayer.highlightStyle = new SuperMap.ThemeStyle({
            stroke: true,
            strokeWidth: 4,
            strokeColor: 'blue',
            fillColor: "#00EEEE",
            fillOpacity: 0.8
        });

        // 用于单值专题图的属性字段名称
        themeLayer.themeField = "POP_DENSITY99";
        // 风格数组，设定值对应的样式
        themeLayer.styleGroups = [{
            start: 0,
            end: 0.02,
            style: {
                color: '#FDE2CA'
            }
        }, {
            start: 0.02,
            end: 0.04,
            style: {
                color: '#FACE9C'
            }
        }, {
            start: 0.04,
            end: 0.06,
            style: {
                color: '#F09C42'
            }
        }, {
            start: 0.06,
            end: 0.1,
            style: {
                color: '#D0770B'
            }
        }, {
            start: 0.1,
            end: 0.2,
            style: {
                color: '#945305'
            }
        }];

        themeLayer.on('mousemove', highLightLayer);
        addThemeFeatures();
    }

    function addThemeFeatures() {
        var getFeatureBySQLParams = new SuperMap.GetFeaturesBySQLParameters({
            queryParameter: new SuperMap.FilterParameter({
                name: "Jingjin",
                attributeFilter: "SMID > -1"
            }),
            toIndex: 500,
            datasetNames: ["Jingjin:BaseMap_R"]
        });
        L.supermap.featureService(dataUrl)
            .getFeaturesBySQL(getFeatureBySQLParams, processComplete, SuperMap.DataFormat.ISERVER);

    }

    function processComplete(serviceResult) {
        var result = serviceResult.result;
        if (result && result.features) {
            themeLayer.addFeatures(result.features);
        }
        initLegendView();
        initInfoView();
    }

    //高亮时显示图层信息框的控件
    function initInfoView() {
        infoView = L.control({position: 'bottomright'});
        infoView.onAdd = function () {
            this._div = L.DomUtil.create('div', 'panel panel-primary infoPane');
            $(this._div).css("width", "272px");
            $("<div class='panel-heading'><h5 class='panel-title text-center'>属性表</h5></div>").appendTo(this._div);
            var content = $("<div class='panel-body content'></div>").appendTo(this._div);
            content.css('fontSize', '14px');
            handleMapEvent(this._div, this._map);
            return this._div;
        };

        infoView.update = function (fea) {
            var content = $(".content");
            content.text("");
            if (!fea) {
                return;
            }
            var innerHtml = "ID: " + fea.attributes.SMID + "<br/>";
            innerHtml += "行政区名:" + fea.attributes.NAME + "<br/>";
            innerHtml += "人口密度:" + parseFloat(fea.attributes.POP_DENSITY99).toFixed(5) + "<br/>";
            content.html(innerHtml);
        }
    }

    //图例控件
    function initLegendView() {
        var legendView = L.control({position: 'bottomright'});
        legendView.onAdd = function () {
            this._div = L.DomUtil.create('div', 'panel panel-primary legend ');
            var title = "<div class='panel-heading'><h5 class='panel-title text-center'>图例</h5></div>";
            $(title + "<div class='panel-body text-center' ><table>" +
                "<tr><td class='legendItemHeader'>人口密度(1999年)</td><td class='legendItemValue'>颜色</td></tr>" +
                "<tr> <td class='legendItemHeader'>0    - 0.02</td> <td class='legendItemValue' style='background: #FDE2CA'></td></tr>" +
                "<tr> <td class='legendItemHeader'>0.02 - 0.04</td> <td class='legendItemValue' style='background: #FACE9C'></td> </tr>" +
                "<tr> <td class='legendItemHeader'>0.04 - 0.06</td> <td class='legendItemValue' style='background: #F09C42'></td> </tr>" +
                "<tr> <td class='legendItemHeader'>0.06 - 0.1</td> <td class='legendItemValue' style='background: #D0770B'></td> </tr>" +
                "<tr> <td class='legendItemHeader'>0.1  - 0.2</td> <td class='legendItemValue' style='background: #945305'></td> </tr>" +
                "</table></div>"
            ).appendTo(this._div);
            handleMapEvent(this._div, this._map);
            return this._div;
        };
        legendView.addTo(map);
    }

    function highLightLayer(e) {
        if (e.target && e.target.refDataID) {
            var fea = themeLayer.getFeatureById(e.target.refDataID);
            if (fea) {
                infoView.addTo(map);
                infoView.update(fea);
            }
        } else if (infoView) {
            infoView.remove();
        }
    }

    function handleMapEvent(div, map) {
        if (!div || !map) {
            return;
        }
        div.addEventListener('mouseover', function () {
            map.dragging.disable();
            map.scrollWheelZoom.disable();
            map.doubleClickZoom.disable();
        });
        div.addEventListener('mouseout', function () {
            map.dragging.enable();
            map.scrollWheelZoom.enable();
            map.doubleClickZoom.enable();
        });
    }

</script>
</body>
</html>